<?php
/**
 * @author kevin
 * @date 2016-03-13 14:00:00
 * @desc 订单收藏管理
 */

if (!defined('IN_ECS'))
{
    die('Hacking attempt');
}
$version = '1.0';   //版本号

if ($_POST['api_version'] != $version)      //网店的接口版本低
{
	api_err('0x008', 'a low version api');
}
if(empty($_POST['st'])){
	api_err('0x008', 'param st is Missing');
}
include_once(ROOT_PATH . 'includes/lib_clips.php');
//用户ID检测
if(!isset($GLOBALS['uid'])){
	api_err('0x015', 'You have not logged on');
}
$user_id = $GLOBALS['uid'];
//收藏列表
if($_POST['st'] == 'list'){
	$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
	$size = abs(intval($_POST['size']));
	$size = ($size > 0 && $size < SIZE_MAX)?$size:SIZE_VALUE;
	$count = $GLOBALS['db']->getOne("SELECT COUNT(*) FROM " .$ecs->table('collect_goods').
                                " WHERE user_id='$user_id' ORDER BY add_time DESC");
	$pager = get_pager('user.php', array('act' => 'collection_list'), $count, $page, $size);
	$data['list'] = get_collection_goodsv1($user_id, $pager['size'], $pager['start']);
	$data['page'] = get_app_pager($pager);
}
//添加收藏
if($_POST['st'] == 'add'){
	$goods_id = isset($_POST['goods_id']) ? intval($_POST['goods_id']) : 0;
	if(empty($goods_id) || $goods_id <= 0){
		api_err('0x008', 'goods_id is Error');
	}
	/* 检查是否已经存在于用户的收藏夹 */
	$sql = "SELECT COUNT(*) FROM " .$GLOBALS['ecs']->table('collect_goods') .
		" WHERE user_id='$user_id' AND goods_id = '$goods_id'";
	if ($GLOBALS['db']->GetOne($sql) > 0){
		api_err('0x008', 'This collect is Existed');
	}else{
		/* 存入用户的收藏夹 */
		$time = gmtime();
		$sql = "INSERT INTO " .$GLOBALS['ecs']->table('collect_goods'). " (user_id, goods_id, add_time)" .
				"VALUES ('$user_id', '$goods_id', '$time')";
		if ($GLOBALS['db']->query($sql) === false){
			api_err('0x008', $GLOBALS['db']->errorMsg());
		}else{
			$data = array();
		}
	}
}
//取消收藏 
if($_POST['st'] == 'delete'){
    $goods_id = isset($_POST['goods_id']) ? intval($_POST['goods_id']) : 0;
	if(empty($goods_id) || $goods_id <= 0){
		api_err('0x008', 'goods_id is Error');
	}
	$sql = 'DELETE FROM ' .$ecs->table('collect_goods'). " WHERE goods_id='$goods_id' AND user_id ='$user_id'";
    if ($GLOBALS['db']->query($sql) === false){
		api_err('0x008', $GLOBALS['db']->errorMsg());
	}else{
		$data = array();
	}
}
//st参数错误
if(!isset($data)){
	api_err('0x008');
}
data_back($data, '', RETURN_TYPE);  //返回数据

